package com.wcj.test;

import com.wcj.linktable.ListNode;

/**
 * 给定一个链表，删除链表的倒数第 n 个节点并返回链表的头指针
 */
public class 删除链表的倒数第n个节点 {

    public ListNode removeNthFromEnd (ListNode head, int n) {
        int len = 0;
        ListNode tmp = head;
        while (tmp != null){
            len++;
            tmp = tmp.next;
        }
        if (n == len){
            return head.next;
        }else {
            tmp = head;
            for (int i = 0; i < len - n - 1;i++){
                tmp = tmp.next;
            }
            tmp.next = tmp.next.next;
            return head;
        }


    }
}
